﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace YouTubeStormer
{
    class CommentDetails
    {
        public enum SearchTarget
        {
            ID,
            Author,
            TextSubString
        }

        public static CommentDetail FindComment(CommentDetail[] collection, string[] searchPatterns, SearchTarget searchTarget = SearchTarget.ID)
        {
            foreach (CommentDetails.CommentDetail comment in collection)
            {
                string compareItem = "";
                switch (searchTarget)
                {
                    case SearchTarget.ID:
                        if (comment.ID != null) compareItem = comment.ID;
                        break;
                    case SearchTarget.Author:
                        if (comment.Author != null) compareItem = comment.Author;
                        break;
                    case SearchTarget.TextSubString:
                        if (comment.Text != null) compareItem = comment.Text;
                        break;
                }

                foreach (string searchPattern in searchPatterns)
                {
                    if (compareItem.ToLower() == searchPattern.ToLower())
                    {
                        return comment;
                    }
                }
            }
            return null;
        }

        public static CommentDetail FindComment(CommentDetail[] collection)
        {
            var foundComment = FindComment(collection, Configuration.StripPasswordsFromAccounts(), searchTarget: SearchTarget.Author);

            foreach (CommentDetail comment in collection)
            {
                foreach (string commentLog in Configuration.StringCollectionToArray(Properties.Settings.Default.CommentLog))
                {
                    if (commentLog == comment.ID) 
                        return comment;
                }
            }
            return null;
        }

        public static CommentDetail[] ParseComments(string document)
        {
            string[] commentBlocks = Regex.Split(document, "<li ");
            List<CommentDetail> commentDetails = new List<CommentDetail>();
            foreach (string commentBlock in commentBlocks)
            {
                try
                {
                    var newComment = new CommentDetail();
                    string[] commentBlockLines = commentBlock.Split('\n');
                    for (int i = 0; i < commentBlockLines.Length; i++)
                    {
                        if (commentBlockLines[i].Contains("comment-text")) newComment.Text = commentBlockLines[i + 1].Trim();
                        if (commentBlockLines[i].Contains("data-id")) newComment.ID = commentBlockLines[i].Split('=')[1].Replace("\"", "");
                        if (commentBlockLines[i].Contains("data-author")) newComment.Author = commentBlockLines[i].Split('=')[1].Replace("\"", "");
                        if (commentBlockLines[i].Contains("data-score")) newComment.Score = commentBlockLines[i].Split('=')[1].Replace("\"", "");
                        if (commentBlockLines[i].Contains("data-flagged")) newComment.Spammed = bool.Parse(commentBlockLines[i].Split('=')[1].Replace("\"", ""));
                        if (commentBlockLines[i].Contains("data-tag"))
                        {
                            newComment.Tag = commentBlockLines[i].Split('=')[1].Replace("\"", "");
                            newComment.HighestRated = (newComment.Tag == "R");
                        }

                    }
                    if (newComment.ID != null)
                    {
                        commentDetails.Add(newComment);
                    }
                }
                catch
                {
                    var x = 0;
                }
            }
            return commentDetails.ToArray();
        }

        public class CommentDetail
        {
            public string ID { get; set; }
            public string Author { get; set; }
            public string Score { get; set; }
            public string Tag { get; set; }
            public string Text { get; set; }
            public bool Spammed { get; set; }
            public bool HighestRated { get; set; }
        }
    }
}
